#coding:utf8
#
# Author: CMGS
# Created on 2010-9-26
#

from common import config as _config
from libs.rpc import rpc as _rpc
from libs.session import sessionPool as _sessionPool

class Control(object):
    
    def findMsg(self, sessions, msg):
        if not sessions.has_key(_config.MEM_MODIFIES_NAME):
            return False

        msgList = sessions[_config.MEM_MODIFIES_NAME]
        for m in msgList:
            if msg == _rpc.toJson(m):
                return msgList.index(m)
        return False

    def addMsg(self, sessions, msg):
        msg = _rpc.loadJson(msg)
        if msg is None:
            return False

        if not sessions.has_key(_config.MEM_MODIFIES_NAME):
            sessions[_config.MEM_MODIFIES_NAME] = [msg]
        else:
            sessions[_config.MEM_MODIFIES_NAME].append(msg)
        return True

    def delMsg(self, sessions, msg):
        msg = _rpc.loadJson(msg)
        if msg is None or not sessions.has_key(_config.MEM_MODIFIES_NAME):
            return False

        try:
            return sessions[_config.MEM_MODIFIES_NAME].remove(msg)
        except:
            return False

        return True       

    def getMsg(self, hash, index):
        sessions = self._getSession(hash)
        if sessions is None or not sessions.has_key(_config.MEM_MODIFIES_NAME):
            return None
        try:
            return sessions[_config.MEM_MODIFIES_NAME][index]
        except:
            return None

    def _transJson(self, msg):
        try:
            return _rpc.loadJson(msg)
        except Exception, e:
            return None

    def _getSession(self, hash):
        return _sessionPool.getSessions(hash)

control = Control()
add = control.addMsg
delete = control.delMsg
get = control.getMsg
find = control.findMsg

def cleanLogoutMsg(sessions, username):
    if not sessions.has_key(_config.MEM_MODIFIES_NAME):
        return True
    p = 0
    length = len(sessions[_config.MEM_MODIFIES_NAME])
    while p < length:
        msg = sessions[_config.MEM_MODIFIES_NAME][p]
        try:
            uname = msg['username']
            assert (uname == username)
            sessions[_config.MEM_MODIFIES_NAME].remove(msg)
            p = 0
            length = len(sessions[_config.MEM_MODIFIES_NAME])
        except:
            p += 1
            continue
    return True

def cleanLoginMsg(sessions, number):
    if not sessions.has_key(_config.MEM_MODIFIES_NAME):
        return True
    p = 0
    length = len(sessions[_config.MEM_MODIFIES_NAME])
    while p < length:
        msg = sessions[_config.MEM_MODIFIES_NAME][p]
        try:
            no = str(msg[_config.MODIFY_NO_NAME])
            assert (int(no) == number)
            sessions[_config.MEM_MODIFIES_NAME].remove(msg)
            p = 0
            length = len(sessions[_config.MEM_MODIFIES_NAME])
        except:
            p += 1
            continue
    return True
